package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.UserFreeze;
import com.tanhua.dubbo.api.mongo.FreezeUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@Service(timeout = 100000)
public class FreezeUserApiImpl implements FreezeUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(UserFreeze userFreeze) {
        mongoTemplate.save(userFreeze);
    }

    @Override
    public void deleteByUserId(Integer userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        mongoTemplate.remove(query,UserFreeze.class);
    }

    @Override
    public List<UserFreeze> findAll() {
        List<UserFreeze> all = mongoTemplate.findAll(UserFreeze.class);
        return all;
    }

    @Override
    public Integer findByUserId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        UserFreeze user = mongoTemplate.findOne(query, UserFreeze.class);
        if (user == null){
            return 0;
        }
        return user.getFreezingRange();
    }
}
